/**
 * Copyright (C) 2022 - 长期
 * All rights reserved.
 * <p>
 * 版权所有 (C) 中国烟草总公司
 */
package com.itzhoun.controller;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.itzhoun.exception.UnauthorizedException;
import com.itzhoun.model.base.Result;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhounan
 * @date 2024/01/23 21:46
 * @Description
 */
@RestControllerAdvice
public class ExceptionController {

    //这里我想针对 AuthenticationException 异常做拦截

    //捕捉shiro异常
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ShiroException.class)
    public Result<String> handle401(ShiroException e){
        return new Result<>(false, "Shiro异常", e.getMessage());
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result<String> handle401(){
        return new Result<>(false, "UnauthenticatedException", null);
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result<String> handle402(AuthenticationException e){
        return new Result<>(false, "AuthenticationException", e.getMessage());
    }

    // 捕捉其他所有异常
    @ExceptionHandler(JWTDecodeException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result globalException(HttpServletRequest request, Throwable ex) {
        return new Result<>(false, "其他异常", null);
    }
}
